<?php
include_once($_SERVER['DOCUMENT_ROOT']."/config.php");
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/film.php');
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/film_name_translation.php');
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/country_name_translation.php');
include_once($gDOCUMENT_ROOT_SYSTEM.'include/dao/film_rating.php');
include_once($gDOCUMENT_ROOT_SYSTEM.'templates/include/addon.php');
class filmAction
{
	private $film;
	private $film_name_translation;
	private $film_rating;
	private $country_name_translation;
	function __construct()
	{
		$this->film = new film();
		$this->film_name_translation = new film_name_translation();
		$this->film_rating = new film_rating();
		$this->country_name_translation = new country_name_translation();
	}
	public function addFilmByDoubanId($data)
	{
		try
		{
			if(isset($data['imdb_id'])&&!empty($data['imdb_id']))
			{
				if(stripos($data['imdb_id'],"tt")!==false)
				{
					$data['imdb_id'] = str_replace('tt','',$data['imdb_id']);
				}
			}
			$where = array(
				'douban_id' => $data['douban_id'],
			);
			$film_id = $this->film->insert($data);
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		
		try
		{
			$data_name['film_id'] = $film_id;
			$data_name['en-US'] = trim($data['film_name_en-US']);
			$data_name['yue-Hans'] = trim($data['film_name_yue-Hans']);
			$data_name['yue-Hant'] = trim($data['film_name_yue-Hant']);
			$data_name['zh-Hans-CN'] = trim($data['film_name_zh-Hans-CN']);
			$data_name['zh-Hant-HK'] = trim($data['film_name_zh-Hant-HK']);
			$data_name['zh-Hant-TW'] = trim($data['film_name_zh-Hant-TW']);
			$count = $this->film_name_translation->insert($data_name);
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		
		try
		{
			$data_rating['film_id'] = $film_id;
			$data_rating['CN'] = intval($data['film_rating_CN']);
			$data_rating['HK'] = intval($data['film_rating_HK']);
			$data_rating['TW'] = intval($data['film_rating_TW']);
			$data_rating['US'] = intval($data['film_rating_US']);
			$count = $this->film_rating->insert($data_rating);
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		return true;
	}
	
	public function editFilmByDoubanId($data)
	{
		try
		{
			if(isset($data['imdb_id'])&&!empty($data['imdb_id']))
			{
				if(stripos($data['imdb_id'],"tt")!==false)
				{
					$data['imdb_id'] = str_replace('tt','',$data['imdb_id']);
				}
			}
			$where = array(
				'douban_id' => $data['douban_id'],
			);
			$film_exist = $this->isFilmExist($where);
			if($film_exist)
			{
				$film_id = $this->getFilmIdByDoubanId($data['douban_id']);
				$result = $this->film->updateById($film_id,$data);
			}
			else
			{
				$film_id = $this->film->insert($data);
			}
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		
		try
		{
			$data_name['film_id'] = $film_id;
			$data_name['en-US'] = trim($data['film_name_en-US']);
			$data_name['yue-Hans'] = trim($data['film_name_yue-Hans']);
			$data_name['yue-Hant'] = trim($data['film_name_yue-Hant']);
			$data_name['zh-Hans-CN'] = trim($data['film_name_zh-Hans-CN']);
			$data_name['zh-Hant-HK'] = trim($data['film_name_zh-Hant-HK']);
			$data_name['zh-Hant-TW'] = trim($data['film_name_zh-Hant-TW']);
			if($film_exist)
			{
				$count = $this->film_name_translation->updateByFilmId($film_id,$data_name);
			}
			else
			{
				$count = $this->film_name_translation->insert($data_name);
			}
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		
		try
		{
			$data_rating['film_id'] = $film_id;
			$data_rating['CN'] = intval($data['film_rating_CN']);
			$data_rating['HK'] = intval($data['film_rating_HK']);
			$data_rating['TW'] = intval($data['film_rating_TW']);
			$data_rating['US'] = intval($data['film_rating_US']);
			if($film_exist)
			{
				$count = $this->film_rating->updateByFilmId($film_id,$data_rating);
			}
			else
			{
				$count = $this->film_rating->insert($data_rating);
			}
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
		return true;
	}
	
	public function getFilmDetail($where,$column='*',$written_lang_tag='zh-Hans-CN')
	{
		try
		{
			if(isset($where['imdb_id'])&&!empty($where['imdb_id']))
			{
				if(stripos($where['imdb_id'],"tt")!==false)
				{
					$where['imdb_id'] = str_replace('tt','',$where['imdb_id']);
				}
			}
			$film_info = $this->film->select($where,$column);
			if(isset($film_info['imdb_url']))
			{
				$film_info['imdb_url'] = addon::getImdbUrl($film_info['imdb_id']);
			}
			if(isset($film_info['imdb_url']))
			{
				$film_info['douban_url'] = addon::getMovieDoubanUrl($film_info['douban_id']);
			}
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}

		try
		{
			$film_info['name'] = $this->film_name_translation->getNameByFilmId($film_info['id'],$column);
			
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			//return false;
		}

		try
		{
			$film_info['country_name'] = $this->country_name_translation->getNameByCountryIdAndTag($film_info['country_id'],$written_lang_tag);
			
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			//return false;
		}
		
		try
		{
			$film_info['rating'] = $this->film_rating->getRatingByFilmId($film_info['id']);
			
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			//return false;
		}
		
		return $film_info;
	}

	public function isFilmExist($where)
	{
		try
		{
			if(isset($where['imdb_id'])&&!empty($where['imdb_id']))
			{
				if(stripos($where['imdb_id'],"tt")!==false)
				{
					$where['imdb_id'] = str_replace('tt','',$where['imdb_id']);
				}
			}
			$film_info = $this->film->select($where,'id');
			if(empty($film_info))
			{
				return false;
			}
			else
			{
				return true;
			}
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
	}

	public function getFilmIdByDoubanId($douban_id)
	{
		try
		{
			$where = array('douban_id'=>$douban_id);
			$result = $this->film->select($where,'id');
			if(empty($result))
			{
				return false;
			}
			else
			{
				return $result['id'];
			}
		}
		catch(Exception $e)
		{
			//echo 'Message: ' .$e->getMessage();
			return false;
		}
	}
}

?>
